package com.example.content.fragment.me

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.content.*
import com.example.content.adapter.recycle.BindListener
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.entity.ItemEntity
import com.example.content.databinding.SecondListLayoutBinding
import com.example.content.ext.convertUrl
import com.example.content.fragment.BaseMvvmFragment
import com.example.content.manager.ActivityMgr
import com.example.content.manager.GlobalMgr
import com.example.content.repository.bean.FollowReq
import com.example.content.repository.bean.UserInfo
import com.example.content.viewmodule.me.MyPageCommonViewModel

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/8
 */
class FollowersListFragment : BaseMvvmFragment<SecondListLayoutBinding, MyPageCommonViewModel>() {

    private lateinit var adapter: RecyclerAdapter<UserInfo>
    var followerType = -1

    override fun initView() {
        if (arguments != null) {
            followerType = arguments!!.getInt(FOLLOWERS_TYPE, -1)
        }
        if (followerType == MY_FANS) {
            dataBinding.titleBar.title.text = getString(R.string.fans)
        } else if (followerType == MY_FOLLOW) {
            dataBinding.titleBar.title.text = getString(R.string.follow)
        }
        adapter = RecyclerAdapter(object : BindListener<UserInfo> {
            override fun convert(holder: BaseViewHolder, item: ItemEntity<UserInfo>) {
                val image = holder.getView<ImageView>(R.id.head_icon)
                context?.let { Glide.with(it).load(item.data.avatar.convertUrl()).into(image) }
                val title = holder.getView<TextView>(R.id.nick_name)
                title.text = item.data.nickname
                val personalDesc = holder.getView<TextView>(R.id.personal_des)
                personalDesc.text = item.data.signature
                val followBtn = holder.getView<TextView>(R.id.follow_btn)
                followBtn.setOnClickListener {
                    val isFollow = (item.data.follow_related != 0)
                    val req = FollowReq(item.data.id, if (isFollow) UN_ACTION else DO_ACTION)
                    viewModel.follow(req)
                }
                when (item.data.follow_related) {
                    FOLLOW_UN -> {
                        followBtnChange(followBtn, R.drawable.ic_follow, R.string.follow_me, R.color.white)
                    }
                    FOLLOW_ALREADY -> {
                        followBtnChange(followBtn, R.drawable.ic_alread_follow, R.string.already_follow, R.color.followTextColor)
                    }
                    FOLLOW_EACH -> {
                        followBtnChange(followBtn, R.drawable.ic_alread_follow, R.string.follow_each_other, R.color.followTextColor)
                    }
                }
            }
        })
        adapter.addItemViewByType(MY_FOLLOWERS_ITEM_TYPE, R.layout.item_user_layout)
        dataBinding.recyclerView.adapter = adapter
        dataBinding.recyclerView.layoutManager = LinearLayoutManager(context)
    }

    override fun queryData() {
        if (followerType != -1) {
            viewModel.getFollowers(followerType)
        }
    }

    private fun followBtnChange(view:TextView, @DrawableRes resId: Int, @StringRes text: Int, @ColorRes color:Int) {
        view.background =
            context?.let { it1 -> ContextCompat.getDrawable(it1, resId) }
        view.text = context?.getString(text)
        view.setTextColor(ContextCompat.getColor(context!!, color))
    }

    override fun initListener() {
        dataBinding.titleBar.backIcon.setOnClickListener {
            activity?.finish()
        }
        viewModel.followersData.observe(this) { list ->
            list?.let { innerList ->
                innerList.forEach {
                    adapter.data.add(ItemEntity(MY_FOLLOWERS_ITEM_TYPE, it))
                }
                adapter.notifyDataSetChanged()
            }
        }
        adapter.setOnItemClickListener { _, view, position ->
            val bundle = Bundle()
            bundle.putInt(USER_ID, adapter.data[position].data.id)
            bundle.putInt(KEY_FRAGMENT, PERSONAL_INFO_CENTER)
            ActivityMgr.startFragment(bundle)
        }

        GlobalMgr.follow.observe(this, { followResp ->
            for (itemData in adapter.data) {
                if (followResp.follow == itemData.data.id) {
                    itemData.data.follow_related = followResp.follow_related
//                    if (followResp.follow_related == FOLLOW_UN) {
//                        dataBinding.followMe.text = this.getString(R.string.follow)
//                        dataBinding.followMe.setTextColor(Color.parseColor("#FF4C6A"))
//                    } else {
//                        dataBinding.followMe.text = this.getString(R.string.already_follow)
//                        dataBinding.followMe.setTextColor(Color.parseColor("#999999"))
//                    }
                    val position = adapter.data.indexOf(itemData)
                    val view = dataBinding.recyclerView.layoutManager?.findViewByPosition(position)
                    val followBtn = view?.findViewById<TextView>(R.id.follow_btn)
                    when (followResp.follow_related) {
                        FOLLOW_UN -> {
                            if (followBtn != null) {
                                followBtnChange(
                                    followBtn,
                                    R.drawable.ic_follow,
                                    R.string.follow_me,
                                    R.color.white
                                )
                            }
                        }
                        FOLLOW_ALREADY -> {
                            if (followBtn != null) {
                                followBtnChange(
                                    followBtn,
                                    R.drawable.ic_alread_follow,
                                    R.string.already_follow,
                                    R.color.followTextColor
                                )
                            }
                        }
                        FOLLOW_EACH -> {
                            if (followBtn != null) {
                                followBtnChange(
                                    followBtn,
                                    R.drawable.ic_alread_follow,
                                    R.string.follow_each_other,
                                    R.color.followTextColor
                                )
                            }
                        }
                    }
                }
            }
        })
    }

    override fun getLayoutId() = R.layout.second_list_layout

    companion object {
        @JvmStatic
        fun newInstance() = FollowersListFragment()
    }

    override fun getViewModuleClass(): Class<MyPageCommonViewModel> {
        return MyPageCommonViewModel::class.java
    }
}